package com.ruoyi.fjzt.service.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fjzt.mapper.TbFjglFjzt1Mapper;
import com.ruoyi.fjzt.domain.TbFjglFjzt1;
import com.ruoyi.fjzt.service.ITbFjglFjzt1Service;

/**
 * 房间状态Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-18
 */
@Service
public class TbFjglFjzt1ServiceImpl implements ITbFjglFjzt1Service 
{
    @Autowired
    private TbFjglFjzt1Mapper tbFjglFjzt1Mapper;

    /**
     * 查询房间状态
     * 
     * @param ID 房间状态主键
     * @return 房间状态
     */
    @Override
    public TbFjglFjzt1 selectTbFjglFjzt1ByID(Long ID)
    {
        return tbFjglFjzt1Mapper.selectTbFjglFjzt1ByID(ID);
    }

    /**
     * 查询房间状态列表
     * 
     * @param tbFjglFjzt1 房间状态
     * @return 房间状态
     */
    @Override
    public List<TbFjglFjzt1> selectTbFjglFjzt1List(TbFjglFjzt1 tbFjglFjzt1)
    {
        return tbFjglFjzt1Mapper.selectTbFjglFjzt1List(tbFjglFjzt1);
    }

    /**
     * 新增房间状态
     * 
     * @param tbFjglFjzt1 房间状态
     * @return 结果
     */
    @Override
    public int insertTbFjglFjzt1(TbFjglFjzt1 tbFjglFjzt1)
    {
        return tbFjglFjzt1Mapper.insertTbFjglFjzt1(tbFjglFjzt1);
    }


    /**
     * 修改房间状态
     * 
     * @param tbFjglFjzt1 房间状态
     * @return 结果
     */
    @Override
    public int updateTbFjglFjzt1(TbFjglFjzt1 tbFjglFjzt1)
    {

        //发送数据到xx
        String apiUrl = "http://tqrcg5.natappfree.cc/test"; // 接口的URL
//        tbFjglFjzt1.setFlag(1);
        System.out.println(tbFjglFjzt1);
        String jsonData = JSON.toJSONString(tbFjglFjzt1);
        System.out.println(jsonData);

        try {
            // 创建URL对象
            URL url = new URL(apiUrl);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json");
            // 允许输出数据
            connection.setDoOutput(true);
            // 获取输出流并写入请求体
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonData.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 发送请求并获取响应
            int responseCode = connection.getResponseCode();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 处理响应结果
            System.out.println("Response Code: " + responseCode);
            System.out.println("Response Body: " + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }


        return tbFjglFjzt1Mapper.updateTbFjglFjzt1(tbFjglFjzt1);
    }

    /**
     * 批量删除房间状态
     * 
     * @param IDs 需要删除的房间状态主键
     * @return 结果
     */
    @Override
    public int deleteTbFjglFjzt1ByIDs(Long[] IDs)
    {
        return tbFjglFjzt1Mapper.deleteTbFjglFjzt1ByIDs(IDs);
    }

    /**
     * 删除房间状态信息
     * 
     * @param ID 房间状态主键
     * @return 结果
     */
    @Override
    public int deleteTbFjglFjzt1ByID(Long ID)
    {
        return tbFjglFjzt1Mapper.deleteTbFjglFjzt1ByID(ID);
    }

    @Override
    public TbFjglFjzt1 selectTbFjglFjzt1ListWSD() {


        return tbFjglFjzt1Mapper.selectWSD();
    }

    @Override
    public int updateTbFjglFjzt12(TbFjglFjzt1 w) {
        return tbFjglFjzt1Mapper.updateTbFjglFjzt12(w);
    }
}
